#include "main.h"
#include "stmflash.h"
void get_chipID(void);


/**
* @brief Hardware initialization
* @param
*    
*    
* @return none
*/
void device_init(void)
{
    SystemInit();
    RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO, ENABLE);
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC, ENABLE);
    delay_init();               //滴答定时器
    NVIC_Configuration();       //配置中断分组
    Rtc_Init();                 //RTC初始化
    common_pin_config();        //端口初始化
    time_init();                //定时器初始化
    relay_pin_config();         //继电器引脚初始化
    usart1_init(9600);          //串口1初始化232
    usart3_init(9600);          //串口初始化TTL
    read_all_data();            //读取数据
    wdg_init();                 //看门狗 2S
}


/**
* @brief Get chip serial number
* @param
*    
*    
* @return none
*/
void get_chipID(void)
{
    u32 temp0,temp1,temp2;
    temp0 = *(__IO u32*)(0x1FFFF7E8);    //产品唯一身份标识寄存器（96位）
    temp1 = *(__IO u32*)(0x1FFFF7EC);
    temp2 = *(__IO u32*)(0x1FFFF7F0);
                                  
//ID码地址： 0x1FFFF7E8   0x1FFFF7EC  0x1FFFF7F0 ，只需要读取这个地址中的数据就可以了。
 
    device_id[0] = (u8)(temp0 & 0x000000FF);
    device_id[1] = (u8)((temp0 & 0x0000FF00)>>8);
    device_id[2] = (u8)((temp0 & 0x00FF0000)>>16);
    device_id[3] = (u8)((temp0 & 0xFF000000)>>24);
    device_id[4] = (u8)(temp1 & 0x000000FF);
    device_id[5] = (u8)((temp1 & 0x0000FF00)>>8);
    device_id[6] = (u8)((temp1 & 0x00FF0000)>>16);
    device_id[7] = (u8)((temp1 & 0xFF000000)>>24);
    device_id[8] = (u8)(temp2 & 0x000000FF);
    device_id[9] = (u8)((temp2 & 0x0000FF00)>>8);
    device_id[10] = (u8)((temp2 & 0x00FF0000)>>16);
    device_id[11] = (u8)((temp2 & 0xFF000000)>>24);         
}

/**
* @brief Port initialization
* @param
*    
*    
* @return none
*/
void common_pin_config(void)
{
    //GPIO端口设置
    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);    //时钟

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; 
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;    
    GPIO_Init(GPIOA, &GPIO_InitStructure);
}

